/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "../../stdafx.h"
#include "../../include/Label/FCLabel.h"

namespace FaceCat{
	FCLabel::FCLabel(){
		m_textAlign = FCContentAlignment_TopLeft;
		setAutoSize(true);
		setBackColor(FCColor_None);
		setBorderColor(FCColor_None);
		FCSize size = {100, 20};
		m_size = size;
	}

	FCLabel::~FCLabel(){
	}

	FCContentAlignment FCLabel::getTextAlign(){
		return m_textAlign;
	}

	void FCLabel::setTextAlign(FCContentAlignment textAlign){
		m_textAlign = textAlign;
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	String FCLabel::getViewType(){
		return L"Label";
	}

	void FCLabel::getAttribute(const String& name, String *value, String *type){
		if (name == L"textalign"){
            *type = L"enum:FCContentAlignment";
			*value = FCTran::contentAlignmentToStr(getTextAlign());
        }
		else{
			FCView::getAttribute(name, value, type);
		}
	}

	ArrayList<String> FCLabel::getAttributeNames(){
		ArrayList<String> attributeNames = FCView::getAttributeNames();
		attributeNames.add(L"TextAlign");
		return attributeNames;
	}

	void FCLabel::onPaintForeground(FCPaint *paint, const FCRect& clipRect){
		String text = getText();
		if ((int)text.length() > 0){
			int width = getWidth(), height = getHeight();
			if(width > 0 && height > 0){
				FCFont *font = getFont();
				FCSize tSize = paint->textSize(text, font, -1);
				FCPoint tPoint = {(width - tSize.cx) / 2, (height - tSize.cy) / 2};
				FCPadding padding = getPadding();
                switch (m_textAlign){
                    case FCContentAlignment_BottomCenter:
                        tPoint.y = height - tSize.cy;
                        break;
                    case FCContentAlignment_BottomLeft:
                        tPoint.x = padding.left;
                        tPoint.y = height - tSize.cy - padding.bottom;
                        break;
                    case FCContentAlignment_BottomRight:
                        tPoint.x = width - tSize.cx - padding.right;
                        tPoint.y = height - tSize.cy - padding.bottom;
                        break;
                    case FCContentAlignment_MiddleLeft:
                        tPoint.x = padding.left;
                        break;
                    case FCContentAlignment_MiddleRight:
                        tPoint.x = width - tSize.cx - padding.right;
                        break;
                    case FCContentAlignment_TopCenter:
                        tPoint.y = padding.top;
                        break;
                    case FCContentAlignment_TopLeft:
                        tPoint.x = padding.left;
                        tPoint.y = padding.top;
                        break;
                    case FCContentAlignment_TopRight:
                        tPoint.x = width - tSize.cx - padding.right;
                        tPoint.y = padding.top;
                        break;
                }
				FCRect tRect = {tPoint.x, tPoint.y, tPoint.x + tSize.cx, tPoint.y + tSize.cy};
				Long textColor = getPaintingTextColor();
				if(autoEllipsis() && (tRect.right > clipRect.right || tRect.bottom > clipRect.bottom)){
					if (tRect.right > clipRect.right) {
                        tRect.right = clipRect.right;
                    }
                    if (tRect.bottom > clipRect.bottom) {
                        tRect.bottom = clipRect.bottom;
                    }
					paint->drawTextAutoEllipsis(text, textColor, font, tRect);
				}
				else{
					paint->drawText(text, textColor, font, tRect, -1);
				}
			}
		}
	}

	bool FCLabel::onPrePaint(FCPaint *paint, const FCRect& clipRect){
		bool handle = FCView::onPrePaint(paint, clipRect);
		if(!handle){
			if (autoSize()){
				String text = getText();
				int width = getWidth(), height = getHeight();
				if(width > 0 && height > 0){
					FCFont *font = getFont();
					FCSize tSize = paint->textSize(getText(), font, -1);
					int newW = tSize.cx + 4;
					int newH = tSize.cy + 4;
					if (newW != width || newH != height){
						FCSize newSize = {newW, newH};
						setSize(newSize);
						width = newW;
						height = newH;
					}
				}
			}
		}
		return handle;
	}

	void FCLabel::setAttribute(const String& name, const String& value){
		if (name == L"textalign"){
			setTextAlign(FCTran::strToContentAlignment(value));
        }
		else{
			FCView::setAttribute(name, value);
		}
	}
}